package net.hn.hnms.biz.emgcy.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.emgcy.domain.DutyRecordPo;
import net.hn.hnms.biz.emgcy.domain.DutyWorkPo;
import net.hn.hnms.biz.emgcy.domain.dto.DutyWorkDto;
import net.hn.hnms.biz.emgcy.domain.vo.DutyWorkVo;
import net.hn.hnms.biz.emgcy.service.DutyRecordService;
import net.hn.hnms.biz.emgcy.service.DutyWorkService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: baijiajun
 * @Date: 2022/9/14
 */
@Api(value = "事故件事模块", tags = {"事故事件"})
@RestController
@RequestMapping("dutywork")
public class DutyWorkController {
    @Resource
    private DutyWorkService dutyWorkService;
    @Resource
    private DutyRecordService dutyRecordService;
    @ApiOperation("获取事故事件列表")
    @GetMapping("pageList")
    @AutoSetMineCode
    public R<ResponsePage<DutyWorkVo>> pageList(PageQuery pageQuery, DutyWorkDto dutyWorkDto) {
        ResponsePage<DutyWorkVo> list= dutyWorkService.pageList(pageQuery,dutyWorkDto);
        return R.ok(list);
    }

    @ApiOperation("事故事件添加")
    @PostMapping("/add")
    public R add(@RequestBody DutyWorkPo dutyWorkPo){
        if("null".equals(String.valueOf(dutyWorkPo.getDeathNumber()))){
            dutyWorkPo.setDeathNumber(0L);
        }
        if("null".equals(String.valueOf(dutyWorkPo.getWoundNumber()))){
            dutyWorkPo.setWoundNumber(0L);
        }
        if("null".equals(String.valueOf(dutyWorkPo.getMayhemNumber()))){
            dutyWorkPo.setMayhemNumber(0L);
        }
        if (Integer.parseInt(dutyWorkPo.getType()) == 1 || dutyWorkPo.getIsAlarm().intValue() == 0) {
            dutyWorkPo.setReceiveAt(null);
            dutyWorkPo.setAlarmDate(null);
            dutyWorkPo.setEditAt(null);
        }
        if (dutyWorkPo.getIsAlarm().intValue() == 1) {
            dutyWorkPo.setReceiveAt(null);
            dutyWorkPo.setEditAt(null);
            dutyWorkPo.setEventAt(null);
        }
        String s = dutyWorkPo.getEventAddress();
        if(!StringUtils.isEmpty(s)){
            String[] split = s.split(",");
            List<String> strings = Arrays.asList(split);
            dutyWorkPo.setXCoordinate(Double.parseDouble(strings.get(0)));
            dutyWorkPo.setYCoordinate(Double.parseDouble(strings.get(1)));
            dutyWorkPo.setZCoordinate(Double.parseDouble(strings.get(2)));
        }
        boolean save = dutyWorkService.add(dutyWorkPo);
        DutyRecordPo dutyRecordPo = new DutyRecordPo();
        BeanUtils.copyProperties(dutyWorkPo, dutyRecordPo);
        dutyRecordPo.setDutyId(dutyWorkPo.getId());
        dutyRecordPo.setId(null);
        dutyRecordPo.setCreateTime(dutyWorkPo.getEventAt());
        boolean flag = dutyRecordService.add(dutyRecordPo);
        if(flag){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("事故事件修改")
    @PutMapping("/update")
    public R update(@RequestBody DutyWorkPo dutyWorkPo){
        dutyWorkService.update(dutyWorkPo);
        DutyRecordPo dutyRecord = new DutyRecordPo();
        BeanUtils.copyProperties(dutyWorkPo, dutyRecord);
        dutyRecord.setDutyId(dutyWorkPo.getId());
        dutyRecord.setId(null);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dutyRecord.setCreateTime(sdf.format(new Date()));
        boolean flag = dutyRecordService.add(dutyRecord);
        if(flag){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }


    @ApiOperation("事故事件删除")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Long id){
        Boolean flag = dutyWorkService.delete(id);
        if(flag){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("获取预案信息")
    @PostMapping("/plans/{eventType}")
    public R plans(@PathVariable String eventType) {
        return R.ok(dutyWorkService.plans(eventType));
    }
}
